//
//  ShareHelper.m
//  NetEaseFinancial
//
//  Created by 吴玉峰 on 8/31/15.
//  Copyright (c) 2015 吴玉峰. All rights reserved.
//

#import "WYFShareHelper.h"

#import <UIKit/UIKit.h>

#import "WXApi.h"
#import "YXApi.h"
#import "WeiboSDK.h"


@interface WYFShareHelper () <WXApiDelegate, YXApiDelegate, WeiboSDKDelegate>

//@property (strong, nonatomic) NSMutableDictionary *proxyObjects;
@property (nonatomic, copy) NSString* weiboRedirectUrl;

@end

@implementation WYFShareHelper

+ (instancetype)sharedInstance {
    static WYFShareHelper *helper = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        helper = [[WYFShareHelper alloc] init];
    });

    return helper;
}

- (instancetype)init {

    static id obj = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        obj = [super init];
        if (obj) {
            //            self.proxyObjects = [NSMutableDictionary dictionary];
        }

    });
    self = obj;
    return self;
}

+ (void)registerEasyChatWithAppKey:(NSString *)appKey {
    [YXApi registerApp:appKey];
}

+ (void)registerWeChatWithAppKey:(NSString *)appKey withDescription:(NSString *)desc {
    [WXApi registerApp:appKey withDescription:desc];
}

+ (void)registerSinaWeiboWithAppKey:(NSString *)appKey redirectUrl:(NSString*)redirectUrl {
    [WeiboSDK registerApp:appKey];
}

- (BOOL)handleOpenURL:(NSURL *)url {
    return [WXApi handleOpenURL:url delegate:self] || [YXApi handleOpenURL:url delegate:self] ||
           [WeiboSDK handleOpenURL:url delegate:self];
}

///**
// *  初始化第三方组件
// */
//- (void)initShareSDK {
//    [WXApi registerApp:APP_KEY_WEIXIN];
//    [YXApi registerApp:APP_KEY_YIXIN];
//
//    [WeiboSDK enableDebugMode:YES];
//    [WeiboSDK registerApp:APP_KEY_WEIBO];
//}

#pragma mark - WeiboSDKDelegate method
- (void)didReceiveWeiboRequest:(WBBaseRequest *)request {
}

- (void)didReceiveWeiboResponse:(WBBaseResponse *)response {
    if ([response isKindOfClass:WBSendMessageToWeiboResponse.class]) {
        //        NSString *title = NSLocalizedString(@"发送结果", nil);
        //        NSString *message = [NSString stringWithFormat:@"%@: %d\n%@: %@\n%@: %@", NSLocalizedString(@"响应状态", nil),
        //        (int)response.statusCode, NSLocalizedString(@"响应UserInfo数据", nil), response.userInfo,
        //        NSLocalizedString(@"原请求UserInfo数据", nil),response.requestUserInfo];
        //        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
        //                                                        message:message
        //                                                       delegate:nil
        //                                              cancelButtonTitle:NSLocalizedString(@"确定", nil)
        //                                              otherButtonTitles:nil];
        //        WBSendMessageToWeiboResponse* sendMessageToWeiboResponse = (WBSendMessageToWeiboResponse*)response;
        //        NSString* accessToken = [sendMessageToWeiboResponse.authResponse accessToken];
        //        if (accessToken)
        //        {
        //            self.wbtoken = accessToken;
        //        }
        //        NSString* userID = [sendMessageToWeiboResponse.authResponse userID];
        //        if (userID) {
        //            self.wbCurrentUserID = userID;
        //        }
        //        [alert show];
    }
}

#pragma mark - YXApiDelegate method
- (void)onReceiveRequest:(YXBaseReq *)req {
    //    if ([req isKindOfClass:[ShowMessageFromYXReq class]]) {
    //        ShowMessageFromYXReq* reciveReq = (ShowMessageFromYXReq*)req;
    //        if(reciveReq.message != nil && [reciveReq.message.mediaObject isKindOfClass:[YXAppExtendObject class]]){
    //            YXAppExtendObject* msg = (YXAppExtendObject*)reciveReq.message.mediaObject;
    //            [self showAlert:[NSString stringWithFormat:@"url:%@\nextendinfo:%@", msg.url, msg.extInfo]];
    //        }
    //        NSLog(@"%d", reciveReq.type);
    //    }
}

- (void)onReceiveResponse:(YXBaseResp *)resp {
    if ([resp isKindOfClass:[SendMessageToYXResp class]]) {
        //        SendMessageToYXResp *sendResp = (SendMessageToYXResp *)resp;
        //        NSLog(@"%d, %@", sendResp.code, sendResp.errDescription);
    }
}

#pragma mark - WXApiDelegate method
- (void)onReq:(BaseReq *)req {
}

- (void)onResp:(BaseResp *)resp {
    if ([resp isKindOfClass:[SendMessageToWXResp class]]) {
        NSString *strTitle = [NSString stringWithFormat:@"发送媒体消息结果"];
        NSString *strMsg = [NSString stringWithFormat:@"errcode:%d", resp.errCode];

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle
                                                        message:strMsg
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil, nil];
        [alert show];
    }
}

@end
